package testHutools.testReflectUtils;

import cn.hutool.core.util.ReflectUtil;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : hutools.reflectUtils.TestMul
 * @Description : 类描述
 * Created by cyd on 2021-06-26 12:21:35
 */
public class Test01 {

    /**
     * 通过反射获取对象，并设置对象的属性
     */
    @Test
    public void test() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> appleClass = Class.forName("testHutools.testReflectUtils.Apple");
        System.out.println(appleClass);
        Object apple = appleClass.newInstance();
        ReflectUtil.setFieldValue(apple, "color", "red");
        ReflectUtil.setFieldValue(apple, "weight", "200g");
        System.out.println(apple);
    }

    /**
     * 通过反射获取对象，
     * 获取对象的属性，
     * 然后设置对象的属性
     */
    @Test
    public void test02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class<?> appleClass = Class.forName("testHutools.testReflectUtils.Apple");
        System.out.println(appleClass);
        Field color = appleClass.getDeclaredField("color");
        Field weight = appleClass.getDeclaredField("weight");

        Object apple = appleClass.newInstance();
        ReflectUtil.setFieldValue(apple, color, "red");
        ReflectUtil.setFieldValue(apple, weight, "200g");
        System.out.println(apple);
    }

    @Test
    public void test03() {
        ArrayList<Fruit> fruits = new ArrayList<>();
        Apple apple = new Apple();
        fruits.add(apple);
        Fruit fruit = fruits.get(0);
        fruit.setColor("yellow");
        System.out.println(apple);
        System.out.println(apple.equals(fruit));
    }


    @Test
    public void test04() {
        ArrayList<Fruit> fruits = new ArrayList<>();

        Apple apple = new Apple("red", "200g");
        Banana banana = new Banana("yellow", "100");

        fruits.add(apple);
        fruits.add(banana);

        changFieldValue(fruits);
        printList(fruits);
    }

    /**
     * 测试：通过泛型将通用的处理list中的对象
     */
    @Test
    public void test05() {
        ArrayList<Apple> apples = new ArrayList<>();
        apples.add(new Apple("red", "200g"));

        ArrayList<Banana> bananas = new ArrayList<>();
        bananas.add(new Banana("yellow", "100g"));

        changFieldValue(apples);
        changFieldValue(bananas);
        printList(apples);
        printList(bananas);
        
        System.out.println("-----------------");
        Banana banana = bananas.get(0);
        System.out.println(banana);
    }

    private static <T> void printList(List<T> list) {
        System.out.println(list);
    }

    /**
     * 通过泛型将通用的处理list中的对象
     * 通过反射获取list中对象的属性
     * 通过反射设置list中对象的属性
     */
    private static <T> void changFieldValue(List<T> list) {
        list.forEach(obj -> {
                    if ("red".equals(ReflectUtil.getFieldValue(obj, "color"))) {
                        ReflectUtil.setFieldValue(obj, "color", "blue");
                    }
                    try {
                        ReflectUtil.setFieldValue(obj, "color2", "blue");

                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                    System.out.println(obj.getClass());
                }
        );
    }
}
